<?php
$refPage = "lister.php";
include( "langincluder.php" );

if( !isset($_SESSION['userName']) || empty($_SESSION['userName']) ) $_SESSION['userId'] = -1;

// $user = user ID of the person's uploads
// $perPage = entries per page
// $onPage = page we are on
require("sql/sqlCon.php");

if( !isset( $_GET['u'] ) ) $user = $_SESSION['userId'];
else $user = $_GET['u'];

// default per page goes here
if( !isset( $_GET['perP'] ) || $_GET['perP']<=0 ) $perPage = 20;
else $perPage = $_GET['perP'];

if( !isset( $_GET['onP'] ) ) $onPage = 0;
else $onPage = $_GET['onP'];

$q = mysql_query("SELECT fileId FROM $uploadsTableName WHERE byUserId = '$user'");
$records = mysql_num_rows($q);

echo $strings['totalUploads'].' : '.$records."<br>".$strings['filesPerPage']." : ";

$maxPage = ceil($records / $perPage);
if( $onPage >= $maxPage ) $onPage = $maxPage-1;



$q = mysql_query("SELECT * FROM $uploadsTableName WHERE byUserId = '$user' ORDER BY fileId DESC LIMIT ".$perPage*($onPage+1));

?>

<style type="text/css">
table.m td
{
	padding : 7px;
	border-bottom-style : dashed;
	border-bottom-color : #aaa;
	border-bottom-width : 1px;
	text-align : center;
}

</style>
<script type="text/javascript">
function confirmAndDel(w)
{
	var orly = confirm('<?php echo $strings['confirmDel']; ?>');
	if( !orly ) return;
	
	
	
	var ajax;
	if (window.XMLHttpRequest)
		ajax = new XMLHttpRequest();
	else
		ajax = new ActiveXObject("Microsoft.XMLHTTP");
	
	
	ajax.open("POST", "./uploader/deleter.php", true);
	ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajax.send("fid="+w);
	
	ajax.onreadystatechange = function()
	{
		if( ajax.readyState==4 && ajax.status==200 )
		{
			if( ajax.responseText == 'ok' ) 
			{
				alert('<?php echo $strings['delSuccess']; ?>');
				history.go(0);
				return;
			}
			if( ajax.responseText == 'denied' )
			{
				alert('<?php echo $strings['errDenied']; ?>');
				return;
			}
			if( ajax.responseText == 'fileNotFound' )
			{
				alert('file not found.... hax');
				return;
			}
			alert( '<?php echo $strings['errUnknown']; ?> : '+ajax.responseText );
		}
	}
}

function dropD()
{
	var dropdown = document.getElementById("ddpp");
    var myindex  = dropdown.selectedIndex;
    var SelValue = dropdown.options[myindex].value;
    
	window.location = "<?php echo '?p='.$_GET['p'].'&onP='.$i.'&u='.$user; ?>&perP="+SelValue;
}

</script>

<select name='pp' id='ddpp' onchange='dropD();'>
<?php
$opts = array(5, 10, 15, 20, 25, 30, 50, 100);
foreach( $opts as $i )
{
	echo "<option".($perPage == $i ? " selected" : "").">".$i."</option>";
}
?>
</select>

<?php

echo "<table class='m'>";
for($i=0; $v = mysql_fetch_array($q); $i++)
{ // mnooo typa implementacia ama ne mi ide na um v momenta nishto
	if($i >= $perPage*$onPage && $i < $perPage*($onPage+1))
	{
		if( $v['isPrivate'] && $user!=$_SESSION['userId'] && $_SESSION['userLevel']!=1 && $_SESSION['userLevel']!=2 )
		{
			echo "<tr><td colspan='5'>".$strings['privateFileListing']."</td></tr>";
			continue;
		}
		
		echo "<tr><td>".( $v['isImg'] ? "<img src='".$v['filePath']."_thumb.jpeg'>" : "" )."<br><a href='".$v['filePath']."'>Link</a></td>".
			 "<td>".$v['origFileName']."<br>".$v['fileSizeKB']."KB<br>".$v['uploadedDate']."</td>".
			 "<td>".$v['description']."</td>".
			 "<td>".( $v['isPrivate'] ? $strings['fileSharingPrivate'] : $strings['fileSharingPublic'] )."</td>";
		if( $user === $_SESSION['userId'] || $_SESSION['userLevel'] == 1 || $_SESSION['userLevel'] == 2 )
			echo "<td><a onclick='confirmAndDel(".$v['fileId'].")'><img src='./uploader/deleteIcon.png' alt='delete'></a></td>";
		echo "</tr>\n";
	}
}
echo "</table>";


for($i = 0; $i < ceil($records / $perPage); $i++)
{
	if( $i == $onPage )
		echo '<a href="?p='.$_GET['p'].'&perP='.$perPage.'&onP='.$i.'&u='.$user.'"><button type="button" class="onThis">'.($i+1).'</button></a>&nbsp ';
	else
		echo '<a href="?p='.$_GET['p'].'&perP='.$perPage.'&onP='.$i.'&u='.$user.'"><button type="button">'.($i+1).'</button></a>&nbsp ';
}

?>